package a;

import com.google.common.collect.Lists;
import com.opencsv.bean.CsvToBean;
import com.opencsv.bean.CsvToBeanBuilder;
import com.opencsv.bean.HeaderColumnNameMappingStrategy;
import lombok.extern.slf4j.Slf4j;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;

/**
 * @author: jzw
 * @Description
 * @date: 2022/10/17 20:13
 */

@Slf4j
public class ReadCSV {


    public static void main(String[] args) {

        String path = "F:\\iot\\d";
        File filePath = new File(path);
        ArrayList<File> list = new ArrayList<>();
        getAllFile(filePath, list);
        for (File file : list) {
            String fileName = file.getName();
            String[] split = fileName.split("_");
            String provinceId = split[split.length - 1].substring(0, 3);
            log.info("读取文件名：{}，所属省份：{}",fileName,provinceId);
            List<info> data = getCsvData(file, ',', info.class);
            int insertDate = insertDate(data, provinceId);
            System.out.println(data);
        }
    }

    public static int insertDate(List<info> list, String provinceId) {
        List<List<info>> partition = Lists.partition(list, 1000);
        int count = 0;
        for (List<info> infoList : partition) {
            //dao层调用
        }
        return count;
    }

    /**
     * 解析csv文件并转成bean
     *
     * @param file  csv文件
     * @param clazz 类
     * @param <T>   泛型
     * @return 泛型bean集合
     */
    public static <T> List<T> getCsvData(File file, char separator, Class<T> clazz) {
        InputStreamReader in;
        try {
            in = new InputStreamReader(new FileInputStream(file), "UTF-8");
            BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(new FileInputStream(file), "GBK"));
            String line = bufferedReader.readLine();
            log.info("文件名：{},表头字段:{}", file.getName(), line);
        } catch (Exception e) {
            log.error("读取csv文件数据失败");
            throw new RuntimeException("读取csv文件数据失败");
        }
        HeaderColumnNameMappingStrategy<T> strategy = new HeaderColumnNameMappingStrategy<>();
        strategy.setType(clazz);

        CsvToBean<T> csvToBean = new CsvToBeanBuilder<T>(in)
                .withSeparator(separator)
                .withQuoteChar('\'')
                .withMappingStrategy(strategy).build();
        return csvToBean.parse();
    }

    /**
     * 获取目录下文件及其子目录文件
     * @param fileInput 读取目标文件目录
     * @param allFileList 所有文件
     */
    public static void getAllFile(File fileInput, List<File> allFileList) {
        // 获取文件列表
        File[] fileList = fileInput.listFiles();
        assert fileList != null;
        for (File file : fileList) {
            if (file.isDirectory()) {
                // 递归处理文件夹
                // 如果不想统计子文件夹则可以将下一行注释掉
                getAllFile(file, allFileList);
            } else {
                // 如果是文件则将其加入到文件数组中
                allFileList.add(file);
            }
        }
    }

}
